import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Spin, Upload, message } from 'antd';
import 'moment/locale/zh-cn';
import { ModalC, UploadC } from '../../../../../comps';
import '../../index.less';

@connect(({ portfolioOptimizationM, loading }) => ({
  portfolioOptimizationM,
  loading,
}))
export default class ImportFileC extends PureComponent {
  componentDidMount() {}

  componentWillUnmount() {
    // 离开前清除models数据
  }

  import = (e) => {
    const { dispatch } = this.props;
    const formData = new FormData();

    formData.append('file', e);

    dispatch({
      type: 'portfolioOptimizationM/uploadFile',
      payload: {
        file: formData,
      },
      callback: (res) => {
        if (res) {
          if (res.code == 0) {
            message.success('导入成功');
            this.props.getImportData(res.data);
          } else {
            message.error(res.msg || '导入失败');
          }
        } else {
          message.error('导入失败');
        }
      },
    });
  };

  render() {
    return (
      <UploadC
        title='导入组合'
        type={2}
        accept='.xlsx,.xls'
        showUploadList={false}
        onUpload={(e) => {
          this.import(e);
        }}
        style={{ display: 'inline' }}
      />
    );
  }
}
